// index.js
// 获取应用实例
const app = getApp();

Page({
	data: {
		timeId:0,
		datalist:[],
		keyword: '',
		page:{
			page:1,
			next:true,
			loading:true,
			valloading:false
		},
		shebei:{
			popup:false,
			param:{
				activeId: 0,
				activeText:'',
			},
			component:false
		},
		jobtype:{
			data:[{
				text: '不限',
				value: 0
			},
			{
				text: '长期',
				value: 2
			},
			{
				text: '替班',
				value: 1
			}],
			activeid:0
		},
		personality: [],
		op:0,
		scrolltop:0,
		cityPopup:false,
		InitCity:{
			popup:false,
			component:false,
			province:'',
			city:'',
			isprovince:1,
			iscity:1
		},
	},
	cityPopupOpen(){
		this.setData({
			"InitCity.popup":true,
			"InitCity.component":true
		});
	},
	ChangeCityVal(val){
		var province = val.detail.province;
		var city = val.detail.city;
		this.cityPopupClose();
		if(province!=this.data.InitCity.province || city!=this.data.InitCity.city){
			this.setData({
				"InitCity.province":province,
				"InitCity.city":city
			});
			this.GetData();
		}
	},
	cityPopupClose(){
		this.selectComponent('#DropdownCityPopup').toggle(false);
		this.setData({
			"InitCity.popup":false
		});
	},
	cityPopupAfterLeave(){
		this.setData({
			"InitCity.component":false
		});
	},

	ShebeiPopupOpen(){
		this.setData({
			"shebei.popup":true,
			"shebei.component":true
		});
		
	},
	ShebeiChangeVal(val){
		var sid = val.detail.sid;
		var text = val.detail.text;
		this.ShebeiPopupClose();
		if(parseInt(this.data.shebei.param.activeId) != parseInt(sid)){
			this.setData({
				"shebei.param.activeId":sid,
				"shebei.param.activeText":text
			});
			this.GetData();
		}
	},
	ShebeiPopupClose(){
		this.selectComponent('#DropdownShebeiPopup').toggle(false);
		this.setData({
			"shebei.popup":false
		});
	},
	ShebeiPopupAfterLeave(){
		this.setData({
			"shebei.component":false
		});
	},
	handleDetail(event) {
		var id = event.currentTarget.dataset.id;
		if(parseInt(id)){
			wx.navigateTo({
				url: '../indexdetail/indexdetail?id='+id,
			});
		}else{
			wx.showToast({
				title: '数据错误，请稍后再试',
				icon: 'error',
				duration: 2000
			});
		}
		
	},
	jobtypechange(value){
		this.setData({
			'jobtype.activeid':value.detail
		})
		this.GetData();
	},
	onClickscreen(event) {
		const { name } = event.currentTarget.dataset;
		var index = this.data.personality.indexOf(name);
		var data = this.data.personality;
		if (index>-1) {
			data.splice(index,1);
		} else {
			data.push(name);
		}
		this.setData({
			personality: data,
		});
		this.GetData();
	},
	onClickscreenop(event) {
		const { name } = event.currentTarget.dataset;
		if (parseInt(this.data.op) == parseInt(name)) {
			this.setData({
				op: 0,
			});
		} else {
			this.setData({
				op: parseInt(name),
			});
		}
		this.GetData();
	},



	keywordChange(){
		clearTimeout(this.timeId) //清除定时器
		this.timeId=setTimeout(()=>{
			this.GetData();
		},800)
	},
	onLoad() {
		if(app.globalData.DefaultCity){
			this.watchProvince();
		}else{
			this.GetData();
			app.watchDefaultCity(this.watchProvince)
		}
	},
	GetData(type){
		if(type=='scroll' && !this.data.page.next){
			return false;
		}
		this.setData({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”
			'page.loading':true
		});
		
		var pdata = this.data;
		var param = {};
		var status = true;
		if(type=='scroll' && parseInt(pdata.page.page)>1){
			param['page'] = parseInt(pdata.page.page);
		}else{
			this.setData({
				"page.page":1,
				scrolltop:0
			});
			wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”
				title: '加载中',
				icon: 'loading',
			});
		}
		if(pdata.keyword){
			param['keyword'] = pdata.keyword;
		}
		if(pdata.InitCity.province){
			param['province'] = pdata.InitCity.province;
			param['city'] = pdata.InitCity.city;
		}
		
		if(parseInt(pdata.shebei.param.activeId)>0){
			param['sid'] = pdata.shebei.param.activeId;
		}
		if(parseInt(pdata.jobtype.activeid)>0){
			param['jobtype'] = pdata.jobtype.activeid;
		}
		if(pdata.personality.length){
			if(pdata.personality.indexOf('ishighpay')>-1){
				param['ishighpay'] = 1;
			}
			if(pdata.personality.indexOf('isfare')>-1){
				param['isfare'] = 1;
			}
			if(pdata.personality.indexOf('issafe')>-1){
				param['issafe'] = 1;
			}
		}
		if(parseInt(pdata.op)>0){
			param['op'] = pdata.op;
		}
		wx.request({
			url: 'https://www.grabinter.com/Works/index',
			method: 'POST',
			header: {
				"Content-Type": 'application/x-www-form-urlencoded'
			},
			data:param,
			success: json => {
				if(type=='scroll'){
					var cont = this.data.datalist.concat(json.data.data);
					this.setData({
						'page.valloading':false,
					});
				}else{
					var cont = json.data.data;
					wx.hideLoading();
				}
				
				this.setData({
					'page.page':json.data.page,
					'page.next':json.data.next,
					'page.loading':false,
					datalist:cont
				});
			}
		});
	},
	LoadMore(){
		if(!this.data.page.next){
			return false;
		}
		if(this.data.page.valloading){
			return false
		}
		this.setData({
			'page.valloading':true,
		});
		this.GetData('scroll')
	},
	watchProvince(){
		var val = app.globalData.DefaultCity;
		if(val != 'not'){
			val = val.split(',');
			this.setData({
				"InitCity.province":val[0],
				"InitCity.city":val[1],
			});
		}
		this.GetData();
	},
	GoNavigation(event){
		var index = event.currentTarget.dataset.index;
		var data = this.data.datalist[index];
		wx.openLocation({
		     latitude:Number(data.latitude),
		     longitude:Number(data.longitude),
			 name:data.lastname,
		   })
	},
	onShow: function() {
		
		
	},
})